﻿using CXW.Core.Sharp.Gui;
using System;
using System.Collections;

namespace CXW.Core.Sharp.Services
{
    class DockPanelService
    {
        DockManagerExt dockManager = null;
        public DockPanelService(DockManagerExt dockManager)
        {
            this.dockManager = dockManager;
        }

        public void CreateDockPanel(string addInTreePath, object owner)
        {
            Codon codon = null;
            string type = null, defaultPosition = null;
            foreach (ToolbarItemDescriptor descriptor in AddInTree.BuildItems<ToolbarItemDescriptor>(addInTreePath, owner, false))
            {
                codon = descriptor.Codon;
                type = codon.Properties["type"];
                defaultPosition = codon.Properties.Contains("defaultPosition") ? codon.Properties["defaultPosition"] : "Left";
                if (type != "DockPanel") { throw new NotSupportedException("TreeItem first stage type must DockPanel,current type : " + type); }

                DockPanelExt dockPanelExt = new DockPanelExt(codon);
                dockManager.RegistDockPanel(dockPanelExt, defaultPosition);

                CreateItemFromDockPanel(dockPanelExt, descriptor.SubItems, owner);
            }

            dockManager.InitEvent();
        }

        /// <summary>生成 DockPanel 控件的子项,
        /// DockPanel 的子项有两种情况:
        /// 1,DockPanel 下有一个 NavBarControl 控件,NavBarControl 控件下有多个 NavBarGroup,每个 NavBarGroup 下有一个 TreeView;
        /// 2,DockPanel 下只有一个 TreeView </summary>
        void CreateItemFromDockPanel(DockPanelExt dockPanel, IList subItems, object owner)
        {
            if (subItems.Count > 0)
            {
                if ((subItems[0] as ToolbarItemDescriptor).Codon.Properties["type"] == "NavBarGroup")
                {
                    foreach (ToolbarItemDescriptor des in subItems)
                    {
                        dockPanel.AddGroups(new NavBarGroupExt(des.Codon), new TreeListExt(des.SubItems, owner));
                        //dockPanel.AddGroups(new NavBarGroupExt(des.Codon), new TreeViewExt(des.SubItems, owner));
                    }
                }
                else
                {
                    dockPanel.AddTreeList(new TreeListExt(subItems, owner));
                    //dockPanel.AddTreeView(new TreeViewExt(subItems, owner));
                }
            }
        }
    }
}
